//
//  HZAmotionView.swift
//  weibo002
//
//  Created by 黑眼圈 on 16/4/26.
//  Copyright © 2016年 heiyan. All rights reserved.
//

import UIKit

class HZAmotionView: UIView {

    
    override init(frame: CGRect) {
        super.init(frame: frame)
        
        setupUI()
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    //添加子控件
    private func setupUI(){
        
//        backgroundColor = UIColor.blueColor()
        
        //添加
        addSubview(stackView)
        addSubview(coll)
        addSubview(page)
        
        //约束
        stackView.snp_makeConstraints { (make) -> Void in
            
            make.leading.trailing.bottom.equalTo(0)
            make.height.equalTo(37)
        }
        
        coll.snp_makeConstraints { (make) -> Void in
            
            make.leading.trailing.top.equalTo(0)
            make.bottom.equalTo(stackView.snp_top)
        }
        
        page.snp_makeConstraints { (make) -> Void in
            
            make.bottom.equalTo(stackView.snp_top)
            make.centerX.equalTo(self.snp_centerX)
        }
        
        
        //在主线程空闲的时候执行默认选中默认表情
        dispatch_async(dispatch_get_main_queue()) { () -> Void in
            
            let indexPath = NSIndexPath(forItem: 0, inSection: 1)
            
            //初始化的时候滚动到第一组第一页
             self.coll.scrollToItemAtIndexPath(indexPath, atScrollPosition: UICollectionViewScrollPosition.Left, animated: false)
        
            //初始化的时候 page默认的位置
           self.changePage(indexPath)
            
            //初始化的时候,底部默认的位置
            self.stackView.seleIndex = indexPath.section
        }
        
        
    }
    
    //布局的时候调用
    override func layoutSubviews() {
        super.layoutSubviews()
        
        let itemW = self.bounds.size.width
        let itemH = self.bounds.size.height - stackView.frame.size.height
        
        let flow = coll.collectionViewLayout as! UICollectionViewFlowLayout
        
        flow.itemSize = CGSize(width: itemW, height: itemH)
        
        
        
    }
    
    

    
    //懒加载子控件
    lazy var stackView:HZEmoStackView = {
       
        let stackView = HZEmoStackView(frame: CGRectZero)
        
        
        stackView.tag = 20000
        
        //闭包回调
        stackView.didBtn = { btnType in
            
            //获取组数
            let section = btnType.rawValue
            
            let indexPath = NSIndexPath(forItem: 0, inSection: section)
            
            // 滚动到指定的位置
            self.coll.scrollToItemAtIndexPath(indexPath, atScrollPosition:UICollectionViewScrollPosition.Left, animated: false)
            
            
            //点击时动态改变分页指示器的数据
            self.changePage(indexPath)
        }
        
        return stackView
    }()
    
    
    //动态改变分页指示器的数据
    func changePage(indexPath:NSIndexPath){
        
        //设置总页数
        if indexPath.section == 0{
            
            page.hidden = true
        }else{
            
            page.hidden = false
            page.numberOfPages = HZEmotionVM.sharedEmotion.allEmotion[indexPath.section].count
            page.currentPage = indexPath.item
        }
        
    

        
    }
    
    // MARK: -  滚动的时候调用
    func scrollViewDidScroll(scrollView: UIScrollView) {
        
        
        //算出屏幕中心的偏移量
        let screenTx = UIScreen.mainScreen().bounds.width/2+scrollView.contentOffset.x
        
        let center = CGPoint(x: screenTx, y: 0)

        //获取可见的 cell
        let indexPaths = coll.indexPathsForVisibleItems()
        
        let indexPath:NSIndexPath
        
        if indexPaths.count == 1{
        
             indexPath = indexPaths.first!
            
        }else{
            
            let firItem = coll.cellForItemAtIndexPath(indexPaths.first!)
//            let lasItem = coll.cellForItemAtIndexPath(indexPaths.last!)
            
            
            //判断是否在这个 cell的显示范围之类
            if CGRectContainsPoint(firItem!.frame, center){
                
                indexPath = indexPaths.first!
            }else{
                indexPath = indexPaths.last!
            }
            
        }
        
        //让底部按钮跟着移动
        stackView.seleIndex = indexPath.section
        
        changePage(indexPath)
        
    }


    lazy var coll:UICollectionView = {
        
        //创建布局对象
        let flow = UICollectionViewFlowLayout()
        
       let coll = UICollectionView(frame: CGRectZero, collectionViewLayout: flow)
        
        coll.backgroundColor = UIColor(white: 237/255.0, alpha: 1)
        
        //设置数据源
        coll.dataSource = self
        coll.delegate = self
        
        //关闭弹簧效果
        coll.bounces = false
        //开启分页效果
        coll.pagingEnabled = true
        
        //关闭水平指示器
        coll.showsHorizontalScrollIndicator = false
        
        //去除间距
         flow.minimumInteritemSpacing = 0
        flow.minimumLineSpacing = 0
        flow.scrollDirection = .Horizontal
        
        
        //注册 cell
        coll.registerClass(HZAmotionCell.self, forCellWithReuseIdentifier: "item")
        
        
        return coll
    }()
    
    
    //懒加载分页指示器
    lazy var page:UIPageControl = {
        
        let page = UIPageControl()
        
        //默认隐藏
        page.hidden = true

        page.setValue(UIImage(named: "compose_keyboard_dot_selected"), forKey: "_currentPageImage")
        page.setValue(UIImage(named: "compose_keyboard_dot_normal"), forKey: "_pageImage")
        
        //关闭用户交互
        page.userInteractionEnabled = false
        
        return page
    }()
    
    
    
   
}

// MARK: - coll的数据源方法
extension HZAmotionView:UICollectionViewDataSource,UICollectionViewDelegate {
    
    
    func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        
  
        return HZEmotionVM.sharedEmotion.allEmotion.count
    }
    
    
    func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        
        return HZEmotionVM.sharedEmotion.allEmotion[section].count
    }
    
    func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        
        //取出 cell
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier("item", forIndexPath: indexPath) as! HZAmotionCell
  
//            let ab = HZEmotionVM.sharedEmotion.allEmotion[indexPath.section][indexPath.item]
        
            //传递数据
            cell.amotioArr = HZEmotionVM.sharedEmotion.allEmotion[indexPath.section][indexPath.item]
  
        cell.indexPath = indexPath
        
        //返回 cell
        return cell
    }
    
    
    
    
}












